
package jp.moisture.othello.player;

import java.util.List;

import jp.moisture.othello.board.Board;
import jp.moisture.othello.board.Cell;
import jp.moisture.othello.board.Game.NextListener;
import jp.moisture.othello.board.Stone;

public abstract class Player {
    public interface OnPlayerListener {
        void onStartThinking(Player player);
    }

    public static final int MIND_NONE = 0;
    public static final int MIND_LOOSER = -1;
    public static final int MIND_WINNER = 1;

    private String mName;
    protected int mColor = Stone.BLACK;
    protected Board mBoard;
    private OnPlayerListener mListener;
    protected int mMind = MIND_NONE;

    public Player(String name) {
        mName = name;
    }

    public void setOnPlayerListener(OnPlayerListener listener) {
        mListener = listener;
    }

    public void setColor(int color) {
        mColor = color;
    }

    public int getColor() {
        return mColor;
    }

    public void setBoard(Board board) {
        mBoard = board;
    }

    public abstract Cell next();

    public Thread next(final NextListener listener) {
        List<Cell> cells = mBoard.findPutableCells(mColor);
        if (cells.isEmpty()) {
            listener.notifyNext(null);
            return null;
        }
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                if (mListener != null) {
                    mListener.onStartThinking(Player.this);
                }
                listener.notifyNext(next());
            }
        });
        thread.start();
        return thread;
    }

    @Override
    public String toString() {
        return mName + "(" + new Stone(mColor) + ")";
    }

    public int hearMind() {
        return mMind;
    }
}
